#include "db.hpp"

#define HOST "127.0.0.1" // mysql服务器的地址
#define PORT 3306        // 端口，端口默认就是3306
#define USER "root"      // mysql默认情况下是不允许用户远程登录的，用公网地址是访问不了的
#define PASS "jth951230."
#define DBNAME "gobang"

void mysql_test()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASS, DBNAME, PORT);
    const char *sql = "insert stu values(null,'泰泰',18,53,89,56);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if (ret == false)
    {
        return;
    }
    mysql_util::mysql_destroy(mysql);
}

void json_test()
{
    // 1.将需要进行序列化的数据，存储在Json::Value 对象中
    Json::Value root;
    std::string body;
    root["姓名"] = "泰泰";
    root["年龄"] = 18;
    root["成绩"].append(98); // 注意数组内容存储，需要调用append
    root["成绩"].append(88.5);
    root["成绩"].append(78.5);
    // 序列化
    json_util::serialize(root, body);
    // 调试打印
    DLOG("%s", body.c_str());

    // 反序列化
    Json::Value val;
    json_util::unserialize(body, val);
    // 5.逐个元素去访问Json::Value中的数据
    std::cout << "姓名" << val["姓名"].asString() << std::endl;
    std::cout << "年龄" << val["年龄"].asInt() << std::endl;
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << "成绩" << val["成绩"][i].asFloat() << std::endl;
    }
}

void str_test()
{
    std::string str = ",...,,123,234,,,,,345,,,,";
    std::vector<std::string> arry;
    string_util::split(str, ",", arry);
    for (auto s : arry)
    {
        DLOG("%s", s.c_str());
    }
}

void file_test()
{
    std::string filename="./makefile";
    std::string body;
    file_util::read(filename,body);
    std::cout<<body<<std::endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    user["username"]="jth";
    user["password"]="127456";
    //bool ret =ut.login(user);
    /*bool ret=ut.select_by_name("xiaoming",user);
    std::string body;
    json_util::serialize(user,body);
    std::cout<<body<<std::endl;
    */
    bool ret=ut.win(1);
    //bool ret1=ut.insert(user);
    bool ret2=ut.lose(3);


}
int main()
{
    // json_test();
    //str_test();
    //file_test();
    db_test();

    return 0;
}
